package com.seguriapp.activities;

import java.util.List;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.seguriapp.R;
import com.seguriapp.dialogs.SelectCategoryDialog;
import com.seguriapp.entities.CrimePointEntity;
import com.seguriapp.other.CategoryDialogInterface;
import com.seguriapp.util.Constants;
import com.seguriapp.util.CrimeOverlay;

/**
 * TODO(pjohnson): Insert description
 * 
 * @author Pablo Johnson
 */
public class CrimeMapFragmentActivity2 extends FragmentActivity {

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		getSupportFragmentManager().beginTransaction()
				.add(R.layout.activity_tab_host, MapFragment.newInstance())
				.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
				.commit();
	}

	// @Override
	// protected boolean isRouteDisplayed() {
	// return false;
	// }

	public static class MapFragment extends Fragment implements
			CategoryDialogInterface {

		private View mMapViewContainer;
		private MapView mapView;
		private MapController mapController;
		private ProgressDialog dialog;
		private MyLocationOverlay locationOverlay;

		public static MapFragment newInstance() {
			MapFragment f = new MapFragment();
			return f;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			super.onCreateView(inflater, container, savedInstanceState);
			setHasOptionsMenu(true);
			CrimeActivity mapActivity = (CrimeActivity) getActivity();
			mMapViewContainer = mapActivity.mMapViewContainer;
			mapView = mapActivity.mMapView;
			mapController = mapView.getController();
			mapController.setZoom(Constants.START_ZOOM_LEVEL);

			return mMapViewContainer;
		}

		@Override
		public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
			inflater.inflate(R.menu.select_crime_categories, menu);
			super.onCreateOptionsMenu(menu, inflater);
		}

		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
			case R.id.select_category_menu:
				final FragmentManager fm = getActivity()
						.getSupportFragmentManager();
				final SelectCategoryDialog categoryDialog = new SelectCategoryDialog(
						this, SelectCategoryDialog.CRIME_CATEGORY);
				categoryDialog.show(fm, "category_dialog");
				break;
			default:
				break;
			}
			return super.onOptionsItemSelected(item);
		}

		@Override
		public void onChangeCategories() {
			comenzarDialogo();
			mapView.getOverlays().clear();
			obtenerCrimenes();
			mapView.invalidate();
		}

		@Override
		public void onResume() {
			onChangeCategories();
			super.onResume();
		}

		private void comenzarDialogo() {
			OnCancelListener cancelListener = new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					// getActivity().finish();
				}
			};
			dialog = ProgressDialog.show(getActivity(), "",
					getText(R.string.msgLoading), true, true, cancelListener);
			dialog.show();
		}

		private void obtenerCrimenes() {
//			locationOverlay = new MyLocationOverlay(getActivity(), mapView);
//			locationOverlay.enableMyLocation();
//			locationOverlay.runOnFirstFix(new Runnable() {
//
//				@Override
//				public void run() {
//					GeoPoint location = locationOverlay.getMyLocation();
//					mapController.animateTo(location);
//					final CrimeController cController = new CrimeController(
//							getActivity());
//					final List<CrimePointEntity> items = cController
//							.getCrimeList(
//									cController.getSelectedCategories(true),
//									mapView.getZoomLevel());
//					if (items == null) {
//						getActivity().runOnUiThread(new Runnable() {
//
//							@Override
//							public void run() {
//								Toast.makeText(getActivity(),
//										"No hay categorías seleccionadas.",
//										Toast.LENGTH_SHORT).show();
//
//							}
//						});
//
//					} else {
//						updateLocations(items);
//					}
//					cerrarDialogo();
//				}
//			});
//			mapView.getOverlays().add(locationOverlay);
		}

		public void updateLocations(List<CrimePointEntity> items) {
			final CrimeOverlay afpOverlay = new CrimeOverlay(getActivity(),
					getResources().getDrawable(R.drawable.actualpos), items);
			mapView.getOverlays().add(afpOverlay);
		}

		private void cerrarDialogo() {
			dialog.dismiss();
		}

		@Override
		public void onDestroyView() {
			cancelGetMyLocation();
			super.onDestroyView();
		}

		private void cancelGetMyLocation() {
			locationOverlay.disableMyLocation();
		}

	}
}
